<?php
	if(!isset($_GET['bill_id']))
		die('No Bill ID is given');
	
	@extract($_REQUEST);
	define('JPATH_BASE', dirname(__FILE__) );
	define( 'DS', DIRECTORY_SEPARATOR );
	define( '_JEXEC', 1 );
	require_once(JPATH_BASE.DS."include".DS."arrays.php");
	require_once(JPATH_BASE.DS."include".DS."setup.php");
	require_once(JPATH_BASE.DS."include".DS."functions.php");

	$sql = "select tbl1.company_id, tbl1.document_id, tbl1.bill_no, date_format(tbl1.bill_date, '%d-%m-%Y') as `bill_date`, date_format(tbl1.period_from, '%d-%m-%Y') as `period_from`, date_format(tbl1.period_to, '%d-%m-%Y') as `period_to`, tbl2.companyname, tbl2.companycode, tbl2.surcharge, tbl2.address, tbl2.city, tbl2.state, tbl2.mobile, tbl2.road_charge, tbl2.insurance_charge, tbl2.service_tax from bills as tbl1, companies as tbl2 where tbl1.company_id = tbl2.id and tbl1.id = '".$_GET['bill_id']."'";	
	$result = mysql_query($sql);
	$count = mysql_num_rows($result);
	if($count == 0) {
		die('No Bill exists for this Bill ID');
	} else {
		$row = mysql_fetch_array($result);
		$company_id = $row['company_id'];
		$Company_detail = $row['companyname'];
		$Company_detail .= '<br />'.$row['address'];
		$Company_detail .= '<br />'.$row['city'].' '.$row['state'];
		$Company_detail .= '<br />'.$row['mobile'];
		
		$companycode = $row['companycode'];
		$document_charge = $row['road_charge'];
		$insurance_charge = $row['insurance_charge'];
		$service_tax = $row['service_tax'];
		$document_id = $row['document_id'];
		$surcharge = $row['surcharge'];
		$bill_no = $row['bill_no'];
		$bill_date = $row['bill_date'];
		$period_from = $row['period_from'];
		$period_to = $row['period_to'];
	
		$sql = "select tbl1.id, date_format(tbl1.booking_date, '%d-%m-%Y') as `booking_date`, tbl1.cn_no, tbl1.invoice_no, tbl1.invoice_value, tbl1.weight, tbl1.weight_unit, tbl1.cour_charge, tbl1.total_charge, tbl2.destination from inventories as tbl1, destinations as tbl2 where tbl1.destination_id = tbl2.id and tbl1.company_id = '".$company_id."' and tbl1.document_id = '".$document_id."' and tbl1.booking_date between '".db_date_format($period_from)."' and '".db_date_format($period_to)."' order by tbl1.booking_date, tbl1.cn_no";
		$result = mysql_query($sql);
		$count = mysql_num_rows($result);
		$i = 1;
		if($count == 0) {
			$message = 'No Inventory exists for this Bill ID';
		} else {
			while($row = mysql_fetch_array($result)){
				$serialno[$row['id']] = $i;
				$booking_date[$row['id']] = $row['booking_date'];
				$cn_no[$row['id']] = $row['cn_no'];
				$invoice_no[$row['id']] = $row['invoice_no'];
				$invoice_value[$row['id']] = $row['invoice_value'];
				$destination_id[$row['id']] = $row['destination'];
				$weight[$row['id']] = ($row['weight_unit'] == 1) ? number_format($row['weight']/1000, 3) : $row['weight'];
				$cour_charge[$row['id']] = $row['cour_charge'];
				$total_charge[$row['id']] = $row['total_charge'];
				
				$i++;
			}
		}
		
		$sql = "select * from settings";
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result);
		$pan_no = $row['pan_no'];
		$servicetax_no = $row['servicetax_no'];
		$servicetax_charge = $row['servicetax_charge'];
		$ecess_charge = $row['ecess_charge'];
		$hsecess_charge = $row['hsecess_charge'];
	}

?>
<!DOCTYPE html>
<html dir="ltr">
<head>
<title><?php echo $lang['CompanyNameWithBusiness'] ?></title>
<link rel="stylesheet" href="css/print.css" type="text/css" />
</head>
<body>
    <table border="0" cellspacing="0" cellpadding="0" align="center" width="98%" style="background: url(images/logo.gif) no-repeat">
	<tr>
		<td colspan=2>
			<div style="position:absolute; width:97%; text-align:right;">
				<h4>Mobile: <?php echo $lang['CompanyMobile'] ?></h4>
				<?php echo $lang['CompanyEmail'] ?>
			</div>
		</td>
	</tr>
	<tr>
		<td align=center colspan=2>
			<h1><?php echo $lang['CompanyName'] ?></h1>
			<?php echo $lang['BusinessName'] ?>
			<h5><?php echo $lang['CompanyAddress'] ?></h5>
		</td>
	</tr>
        <tr><td align=center colspan=2>&nbsp;</td></tr>
        <tr><td align=center colspan=2>&nbsp;</td></tr>
	<tr>
		<td class="left-header">
			To <br />
			M/S <?php echo $Company_detail?>
		</td>
		<td align=right>
			Customer Code : <?php echo $companycode?> <br />
			Bill Number : <?php echo $bill_no?> <br />
			Bill Date : <?php echo $bill_date?>
		</td>
	</tr>
	<tr>
		<td colspan=2 class="left-header">
			<?php
				switch($document_id){
					case 1:
        					echo $lang['CourierCharges'];
        					$colspan = 9;
        					break;
        				case 2:
        					echo $lang['CargoCharges'];
        					$colspan = 10;
        					break;
        				case 3:
        					echo $lang['InternationalCharges'];
        					$colspan = 9;
        					break;
				}
			?>
		</td>
	</tr>
	<tr>
		<td class="left-header">
			<h4>Period From : <?php echo $period_from?></h4>
		</td>
		<td>
			<h4>To : <?php echo $period_to?></h4>
		</td>
	</tr>
</table>
<table border="0" cellspacing="0" cellpadding="0" align="center" width="98%" class="print-report">
	<tr>
		<td colspan="<?php echo $colspan ?>"><hr style="border: 1px dashed #000"></td>
	</tr>
	<tr>
		<th align=left>S.N.</th>
		<th align=left>Book Date</th>
		<th align=left>C/N No.</th>
		<th align=left>Destination</th>
		<th align=right>Wt. (Kgs.)</th>
		<th align=right>Ins. Value</th>
		<th align=right>Ins. Charge</th>
		<th align=right>Fright Charge</th>
		<?php
			if($document_id == 2){
				echo "<th align=right>Doc. Charge</th>";
			}	
		?>
		<th align=right>Total</th>
	</tr>
	<tr>
		<td colspan="<?php echo $colspan ?>"><hr style="border: 1px dashed #000"></td>
	</tr>
	<?php
		if(isset($message)){
			echo '<tr>
			<td align=left colspan="'.$colspan.'">'.$message.'</td>
			<tr>';
			exit;
		}	
		$t_weight = 0;
		$t_invoice_value = 0;
		$t_cour_charge = 0;
		$t_document_charge = 0;
		$t_total_charge = 0;
		foreach($serialno as $k => $v){
			$t_weight = $t_weight + $weight[$k];
			$t_invoice_value = $t_invoice_value + $invoice_value[$k];
			$t_invoice_charge = $t_invoice_charge + ($insurance_charge / 100 * $invoice_value[$k]);
			$t_cour_charge = $t_cour_charge + $cour_charge[$k];
			$t_document_charge = $t_document_charge + $document_charge;
			$t_total_charge = $t_total_charge + $total_charge[$k] + ($insurance_charge / 100 * $invoice_value[$k]);
	?>
			<tr>
			<td align=left><?php echo $serialno[$k]?></td>
			<td align=left><?php echo $booking_date[$k]?></td>
			<td align=left><?php echo $cn_no[$k]?></td>
			<td align=left><?php echo $destination_id[$k]?></td>
			<td align=right><?php echo $weight[$k]?></td>
			<td align=right><?php echo $invoice_value[$k]?></td>
			<td align=right><?php echo number_format($insurance_charge / 100 * $invoice_value[$k], 2) ?></td>
			<td align=right><?php echo $cour_charge[$k]?></td>
			<?php
				if($document_id == 2){
					echo "<td align=right>".$document_charge."</td>";
				}	
			?>
			<td align=right><?php echo number_format($total_charge[$k] + ($insurance_charge / 100 * $invoice_value[$k]), 2)?></td>
			</tr>
	<?php
		}
	?>
	<tr>
		<td colspan="<?php echo $colspan ?>"><hr style="border: 1px dashed #000"></td>
	</tr>
	<tr>
		<td colspan=2 align=right>
			Total Picups
		</td>
		<td>
			<?php echo $serialno[$k]?> 
		</td>
		
		<td align=right>
			Subtotal
		</td>
		<td align=right>
			<?php echo number_format($t_weight, 3)?> 
		</td>
		<td align=right>
			<?php echo number_format($t_invoice_value, 2)?>
		</td>
		<td align=right>
			<?php echo number_format($t_invoice_charge, 2)?> 
		</td>
		<td align=right>
			<?php echo number_format($t_cour_charge, 2)?> 
		</td>
		<?php
			if($document_id == 2){
				echo "<td align=right>".number_format($t_document_charge, 2)."</td>";
			}
		?>
		<td align=right><?php echo number_format(round($t_total_charge, 2), 2)?></td>
	</tr>
	<tr>
		<td colspan="<?php echo $colspan ?>"><hr style="border: 1px dashed #000"></td>
	</tr>
	<tr>
		<td colspan="<?php echo ($colspan - 1) ?>" align=center>
			Fuel surcharges @ <?php echo $surcharge?>% on Total
		</td>
		<td align=right>
			<?php 
			$t_surcharge = (($surcharge / 100) * $t_total_charge);
			echo number_format(round($t_surcharge, 2), 2);
			?> 
		</td>
		
	</tr>
	<?php
		if($service_tax == 1){
	?>
	<tr>
		<td colspan="<?php echo ($colspan - 1) ?>" align=center>
			Service Tax @ <?php echo $servicetax_charge?>% on Total Charges
		</td>
		<td align=right>
			<?php 
			$t_servicetax = (($servicetax_charge / 100) * ($t_surcharge + $t_total_charge));
			echo number_format(round($t_servicetax, 2), 2);
			?> 
		</td>
		
	</tr>
	<tr>
		<td colspan="<?php echo ($colspan - 1) ?>" align=center>
			Edu. CESS @ <?php echo $ecess_charge?>% on Service Tax
		</td>
		<td align=right>
			<?php 
			$t_ecesstax = (($ecess_charge / 100) * $t_servicetax);
			echo number_format(round($t_ecesstax, 2), 2);
			?> 
		</td>
		
	</tr>
	<tr>
		<td colspan="<?php echo ($colspan - 1) ?>" align=center>
			Higher Sec. Edu. CESS @ <?php echo $hsecess_charge?>% on Service Tax
		</td>
		<td align=right>
			<?php 
			$t_hsecesstax = (($hsecess_charge / 100) * $t_servicetax);
			echo number_format(round($t_hsecesstax, 2), 2);
			?> 
		</td>
	</tr>
	<?php
		}
	?>
	<tr>
		<td colspan="<?php echo $colspan ?>"><hr style="border: 1px dashed #000"></td>
	</tr>
	<tr>
		<td colspan="<?php echo ($colspan - 1) ?>" align=center>Rounded of Grand Total Payable</td>
		<td align=right>
			<?php
			$t_payable = round(($t_total_charge + $t_surcharge + $t_servicetax + $t_ecesstax + $t_hsecesstax), 2);
			echo number_format($t_payable, 2);
			?>
		</td>
	</tr>
	<tr>
		<td colspan="<?php echo $colspan ?>"><hr style="border: 1px dashed #000"></td>
	</tr>
	<tr>
		<td colspan="<?php echo $colspan ?>">
			In Words : <?php echo amount_word($t_payable)?>
		</td>
	</tr>
	<tr>
		<td colspan="<?php echo $colspan ?>"><hr style="border: 1px dashed #000"></td>
	</tr>
	<tr>
		<td colspan="<?php echo $colspan ?>" style="padding-top:15px">
			TERMS AND CONDITIONS :
			<ol>
				<li>Interset will be charged @ 24% p.a. on overdue unpaid bills.</li>
				<li>Any discrepancy whatsoever out of bill will lapse unless raised in writing in 7 days from the receipt of this bill.</li>
				<li>Payment are to be made by A/c payee cheque in favour of <?php echo strtoupper($lang['CompanyName']) ?>.</li>
				<li>Any dispute subject to Rewari jurisdiction.</li>
			</ol>
		</td>
	</tr>
	<tr>
		<td colspan="<?php echo $colspan ?>" style="padding-top:30px">
			For <?php echo strtoupper($lang['CompanyName']) ?> <br /><br />
			Authorized Signatory
		</td>
	</tr>
	<tr>
		<td colspan=3 style="padding-top:15px">
			<hr style="border: 1px dashed #000">
			PAN Number: <?php echo $pan_no?>
			<hr style="border: 1px dashed #000">
			Sevice Tax Number: <?php echo $servicetax_no?>
			<hr style="border: 1px dashed #000">
		</td>
	</tr>
	<tr>
		<td colspan="<?php echo $colspan ?>" style="padding-top:30px">
			*****This is Computer Generated Bill and is valid even though not Signed*****
		</td>
	</tr>
</table>
</body>
</html>